<template>
  <div class="poster">
    <div class="avatar">
      <img :src="userInfo.avatarUrl" alt="">
    </div>
    <div class="info">
      <div class="personal">
        <div class="name">{{ userInfo.nickname }}</div>
        <div class="more" v-if="!$route.path.startsWith('/otherHome')" @click="$router.push('/myinfo')">更多数据</div>
        <div class="follow" v-else @click="follow" :class="{ followed: isFollowed }">{{ isFollowed ? '已关注' : '关注 +' }}
        </div>
      </div>
      <div class="friend">
        <div class="guanzhu">
          关注: <span>{{ userInfo.follow }}</span>
        </div>
        <div class="fensi">
          粉丝: <span>{{ userInfo.fans }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUpdated } from 'vue';
import { reqFollow, reqIsFollow } from '@/apis/user';
import userStore from '@/stores/modules/user';
let isFollowed = ref(false)
const { userInfo } = defineProps(['userInfo'])
onUpdated(() => {
  if (userInfo.userId != userStore().userId) {
    isFollow()
  }
})
const follow = async () => {
  const res = await reqFollow({ followerId: userStore().userId, followeeId: userInfo.userId })
  if (res.code == 200) {
    isFollowed.value = true
  }
}
const isFollow = async () => {
  const res = await reqIsFollow({ followerId: userStore().userId, followeeId: userInfo.userId })
  if (res.code == 200) {
    if (res.data == true) {
      isFollowed.value = true
    }
  }
}
</script>

<style lang="scss" scoped>
.poster {
  width: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-around;
  align-items: center;
  padding: .1667rem 0;
  font-size: .2667rem;
  margin-top: .1667rem;

  .avatar {
    width: 1rem;
    height: 1rem;
    background-color: #ccc;
    border-radius: 10%;
    overflow: hidden;

    img {
      width: 100%;
    }
  }

  .info {
    width: 60%;
    height: 1rem;
    display: flex;
    flex-direction: column;
    justify-content: space-around;

    .personal {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .more {
        cursor: pointer;
        background-color: #FF9090;
        color: #fff;
        padding: .0667rem;
        border-radius: .05rem;
      }

      .follow {
        background-color: #fc7113;
        border-radius: .0833rem;
        padding: .0833rem;
        font-size: .2667rem;
        color: #fff;
        cursor: pointer;
      }

      .followed {
        background-color: #ccc;
        border-radius: .0833rem;
        padding: .0833rem;
        font-size: .2667rem;
        color: #fff;
        cursor: pointer;
      }
    }

    .friend {
      display: flex;
      justify-content: start;
      gap: .3333rem;
    }
  }
}
</style>